package com.example.administrator.runbook.findfragment.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.findfragment.model.BookMessageList;

import java.util.List;

/**
 * Created by 俊峰 on 2016/11/11.
 */

public class MyListAdapter extends BaseAdapter {

    private Context mContext;
    private List<BookMessageList.MessageList> mMessageLists;
    private LayoutInflater mLayoutInflater;

    public MyListAdapter(Context context, List<BookMessageList.MessageList> messageLists) {
        mContext = context;
        mMessageLists = messageLists;
        mLayoutInflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (mMessageLists != null) {
            ret = mMessageLists.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return mMessageLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = mLayoutInflater.inflate(R.layout.activity_book_list_dialog_item, parent, false);
        }
        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder(ret);
            ret.setTag(holder);
        }
        onBindView(holder, mMessageLists.get(position), position);
        return ret;
    }

    private void onBindView(ViewHolder holder, BookMessageList.MessageList list, int position) {
        boolean vip = list.isIsVip();
        if (!vip) {
            holder.getImageView.setVisibility(View.INVISIBLE);
        } else {
            holder.getImageView.setVisibility(View.VISIBLE);
        }
        holder.mTextView.setText("" + position + ".");
        holder.mTitle.setText(list.getTitle());
        boolean chick = list.getChick();
        if (chick) {
            holder.mTextView.setTextColor(Color.RED);
            holder.mTitle.setTextColor(Color.RED);
        } else {
            holder.mTextView.setTextColor(Color.GRAY);
            holder.mTitle.setTextColor(Color.GRAY);
        }
        boolean here = list.isHere();
        if (here) {
            holder.mImageView.setImageResource(R.mipmap.ic_toc_item_dark_activated);
        } else {
            holder.mImageView.setImageResource(R.mipmap.ic_toc_item_dark_normal);
        }
    }

    private static class ViewHolder{
        private ImageView mImageView;
        private TextView mTextView;
        private TextView mTitle;
        private ImageView getImageView;
        private RelativeLayout itemViewViewById;

        public ViewHolder(View itemView){
            itemViewViewById = (RelativeLayout) itemView.findViewById(R.id.activity_book_list_dialog_item_all);
            mImageView = (ImageView) itemView.findViewById(R.id.activity_book_list_dialog_item_index_img);
            mTextView = (TextView) itemView.findViewById(R.id.activity_book_list_dialog_item_index_des);
            mTitle = (TextView) itemView.findViewById(R.id.activity_book_list_dialog_item_title);
            getImageView = (ImageView) itemView.findViewById(R.id.activity_book_list_dialog_item_img);
        }

    }
}
